package abmb.pages;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import abmb.utils.MBUser;
import abmb.utils.PageLinks;
import abmb.utils.PageParts;
import abmb.utils.Utilities;

/**
 * Servlet implementation class Follow
 * @author Andrea Balboni
 * @version 1.0
 */
public class Follow extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Follow() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Utilities utils=new abmb.utils.Utilities();
		PageParts pp=new abmb.utils.PageParts();
		HttpSession session=request.getSession(true);
		boolean loggedin=utils.isLoggedIn(session);
		ArrayList<MBUser> followableUsers=new ArrayList<MBUser>();
		ArrayList<MBUser> followedUsers=new ArrayList<MBUser>();
		followableUsers.clear();
		followedUsers.clear();
		String userKey=null;
		Integer userID=session.getAttribute("userID")!=null?Integer.parseInt((String)session.getAttribute("userID").toString()):-1;
		if(request.getParameter("uSearch")!=null){
			userKey=request.getParameter("uSearch").toString();
			followableUsers=utils.findFollowableUserListByKey(userKey,userID);
		}else{
			followableUsers=utils.findFollowableUsers(userID);
		}
		if(session.getAttribute("userID")!=null){
			followedUsers=utils.findFollowedUsers(userID);
		}
		response.setContentType("text/html");
		PrintWriter p= response.getWriter();
		p.println(pp.printHTMLHeader("AB-Microblog Home",loggedin));
		p.println(pp.printSidebar(loggedin));
		if(!loggedin){
			p.println(pp.printNoLoginMessage());
		}
		else{
			p.println("<section>");
			p.println("<h1>You are currently following:</h1>");
			if(followedUsers.isEmpty()){
				p.println("You are not following other users");
			}
			else{
				//				p.println("<ul>");
				//
				//				for(MBUser u:followedUsers){
				//					p.println("<li>"+u.uNickName()+" ("+u.uName()+" "+u.uLastName()+" )"+"</li>");
				//				}
				//				p.println("</ul>");
				p.println("<form action=\""+PageLinks.getFollowLink()+"\" method=\"post\">");
				p.println("<fieldset>");
				p.println("<h1>Choose users you want to <i>un</i>-follow</h1>");
				for(MBUser u:followedUsers){
					p.println("<label>"+u.uNickName()+" ("+u.uName()+" "+u.uLastName()+")"+"<input type=\"checkbox\" name=\"userSelectionUnfollow\" value=\""+u.uID()+"\">"+"</label>");
				}
				p.println("</fieldset>");
				p.println("<fieldset>");
				p.println("<input type=\"submit\" value=\"Submit\">");
				p.println("<input type=\"reset\" value=\"Reset\">");
				p.println("</fieldset>");
				p.println("</form>");
			}
			p.println("</section>");
			if(request.getParameter("userSelectionUnfollow")!=null){
				p.println("<section>");
				p.println("<h1>Your choice</h1>");
				String[] userSelectionUnfollowID;
				userSelectionUnfollowID=request.getParameterValues("userSelectionUnfollow");
				if(userSelectionUnfollowID.length>0){
					if(utils.deleteFollowed(userID,userSelectionUnfollowID)){
						response.sendRedirect(response.encodeRedirectURL(PageLinks.getFollowedLink()));
					}
				}
				p.println("</section>");

			}
			p.println(pp.printUserSearchForm());
			p.println("<section>");
			if(followableUsers.isEmpty()){
				p.println("There are no users to follow");
			}
			else{
				p.println("<form action=\""+PageLinks.getFollowLink()+"\" method=\"post\">");
				p.println("<fieldset>");
				p.println("<h1>Choose users you want to follow</h1>");
				for(MBUser u:followableUsers){
					p.println("<label>"+u.uNickName()+" ("+u.uName()+" "+u.uLastName()+")"+"<input type=\"checkbox\" name=\"userSelection\" value=\""+u.uID()+"\">"+"</label>");
				}
				p.println("</fieldset>");
				p.println("<fieldset>");
				p.println("<input type=\"submit\" value=\"Submit\">");
				p.println("<input type=\"reset\" value=\"Reset\">");
				p.println("</fieldset>");
				p.println("</form>");
			}	
			p.println("</section>");

		}
		if(request.getParameter("userSelection")!=null){
			p.println("<section>");
			p.println("<h1>Your choice</h1>");
			String[] userSelectionFollowID=null;
			userSelectionFollowID=request.getParameterValues("userSelection");
			if(userSelectionFollowID.length>0){
				if(utils.addFollowed(userID,userSelectionFollowID)){
					response.sendRedirect(response.encodeRedirectURL(PageLinks.getFollowedLink()));
				}
			}
			p.println("</section>");

		}
		p.println(pp.printFooterSection());
		p.println(pp.printHTMLFooter());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
